"""
return codes:

0: all is OK
1: tests OK but PEP8 mismatch
2: some tests fails
"""
import gettext
import os
from os import system
import sys
import unittest
from kivy.lang import Builder

IND = ' ' * 4


def indent(s, ind_count):
    return IND * ind_count + s.replace('\n', '\n' + IND * ind_count)

PATH = os.path.abspath('.')
LOCALE_PATH = os.path.join(PATH, 'locales')
gettext.bindtextdomain('thehat', LOCALE_PATH)
lang = gettext.translation('thehat', LOCALE_PATH, ['ru_RU'],
                           codeset='utf-8')
lang.install(True)
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/startgame.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/addplayer.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/addwords.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/playersorder.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/fromto.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/gameconfig.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/beforeroundscreen.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/resultsofonepair.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/guesswordscreen.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/gameresults.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/resultsgeneral.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/editplayers.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/loadgame.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/curresults.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/quickgamesettings.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/welcomescreen.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'mainview.kv'))
if len(sys.argv) > 1:
    patt = sys.argv[1]
else:
    patt = ''
tests = unittest.TestLoader().discover('.', pattern='*{}_test.py'.format(patt))
result = unittest.TestResult()
result.buffer = True
tests.run(result)
print '\n' * 30
print
for el in result.errors:
    print 'In {}: '.format(el[0].shortDescription())
    print indent(el[1], 1)
for el in result.failures:
    print 'In {}: '.format(el[0].shortDescription())
    print indent(el[1], 1)
print '\n'
print 'Total discovered {} test cases'.format(tests.countTestCases())
print 'Total {} failures'.format(len(result.failures))
print 'Total {} errors'.format(len(result.errors))
print 'Total {} tests ran'.format(result.testsRun)
print
print 'Files violate PEP8: '
code = system('python pep8.py . -q')
print
print 'PEP8 violations in your changes: '
code1 = system('svn diff | python pep8.py --diff')
if result.errors+result.failures > 0:
    sys.exit(2)
if code != 0:
    sys.exit(1)
